// Copyright (C) 2006 Ed Korsberg

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace GlobalHooksTest
{
    /// <summary>
    /// Set of helper functions for PruneRecentDocs that abstract the details 
    /// and access/control of RecentDoc registry keys
    /// </summary>
    class RecentDocsFormat
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public RecentDocsFormat(RegistryKey key)
        {
            FolderKey = key;
            FileToIndexMapping = new Dictionary<String, String>();
            IndexToFileMapping = new Dictionary<String, String>();
            MruListEx = new List<string>();
            sFileNames = new List<string>();

            //get mru binary list MRUListEx
            byte[] Bytes = (byte[])FolderKey.GetValue("MRUListEx");

            //build MruListEx list
            for (int index = 0; index < Bytes.Length / sizeof(UInt32); index++)
            {
                UInt32 val = RegTools.GetMruEntry(index, ref Bytes);
                string sVal = val.ToString();
                MruListEx.Add(sVal);
            }

            //get list of keyValues under this registry key
            //and create dictionary mapping between filename and indexname
            List<string> slist = RegTools.ExtractKeyValues(FolderKey);
            foreach (string s in slist)
            {
                object obj = FolderKey.GetValue(s);
                string filename = RegTools.ExtractUnicodeStringFromBinary(obj);
                FileToIndexMapping.Add(filename, s);
                IndexToFileMapping.Add(s, filename);
                sFileNames.Add(filename);
            }

            sIndexNames = RegTools.ExtractKeyValues(FolderKey);
        }

        /// <summary>
        /// Application calls this to remove the reference and registry entry for this filename
        /// </summary>
        /// <param name="sFilename"></param>
        public void DeleteThisFile(String sFilename)
        {
            //dictionary lookup, filename to indexname mapping
            try
            {
                String indexName = FileToIndexMapping[sFilename];
                FolderKey.DeleteValue(indexName);   //delete from Registry
                MruListEx.Remove(indexName);   //remove entry from ram based list
                RegTools.RebuildMruList(FolderKey, MruListEx); //update Registry entry
                //FolderKey.Flush();    //really slows down the execution speed

                //and then unlink mapping strings
                FileToIndexMapping.Remove(sFilename);
                IndexToFileMapping.Remove(indexName);
                sFileNames.Remove(sFilename);
                sIndexNames.Remove(indexName);
            }
            catch (ArgumentException)
            {
            }
            catch (KeyNotFoundException)
            {
            }
        }

        /// <summary>
        /// Given an indexName, return its registry keyValue
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns>Registry keyValue as object</returns>
        public object GetKeyValueFromIndexName(String indexName)
        {
            object keyObj = FolderKey.GetValue(indexName, typeof(string));
            return keyObj;
        }

        /// <summary>
        /// Given an filename, return its registry keyValue
        /// </summary>
        /// <param name="filename"></param>
        /// <returns>Registry keyValue as object</returns>
        public object GetKeyValueFromFileName(String filename)
        {
            string s = FileToIndexMapping[filename];
            object keyObj = FolderKey.GetValue(s, typeof(string));
            return keyObj;
        }

        /// <summary>
        /// Returns count of indexName strings
        /// </summary>
        /// <returns>count of indexName strings</returns>
        public int GetIndexCount()
        {
            return sIndexNames.Count;
        }


        //----------------------------------------------------------------
        //----------------------------------------------------------------
        //----------------------------------------------------------------

        /// <summary>
        /// The registry key for this instance, ie ".txt", ".zip", etc...
        /// </summary>
        RegistryKey FolderKey;

        /// <summary>
        /// The list of indexNames in this table, eg 0,1,2,5,6, etc
        /// </summary>
        public readonly List<string> sIndexNames;

        /// <summary>
        /// The list of filenames in this table
        /// </summary>
        public readonly List<string> sFileNames;

        /// <summary>mapping between filename and indexname</summary>
        public readonly Dictionary<String, String> FileToIndexMapping;

        /// <summary>mapping between filename and indexname</summary>
        public readonly Dictionary<String, String> IndexToFileMapping;

        /// <summary>binary encoded Uint32 list of MRUListEx</summary>
        private List<string> MruListEx;
    }
}
